package org.example.controller;

import com.sun.management.OperatingSystemMXBean;
import org.example.utils.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/system")
public class SystemController {
    @GetMapping("/info")
    public Result getSystemInfo() {
        Map<String, Double> map = new HashMap<>();
        File file = new File("C:/");
        long totalSpace = file.getTotalSpace();
        long freeSpace = file.getFreeSpace();
        double diskUsage = ((totalSpace - freeSpace) * 100.0) / totalSpace;
        map.put("diskUsage", diskUsage);
        OperatingSystemMXBean osBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);
        double cpuUsage = osBean.getSystemCpuLoad() * 100;
        map.put("cpuUsage",cpuUsage);
        // 获取内存使用率
        long totalMemory = osBean.getTotalPhysicalMemorySize();
        long freeMemory = osBean.getFreePhysicalMemorySize();
        double memoryUsage = ((totalMemory - freeMemory) * 100.0) / totalMemory;
        map.put("memoryUsage", memoryUsage);
        return Result.success(map);
    }
}
